package com.hzcx.app.simplechat.util;

import android.content.Context;

import com.google.gson.Gson;
import com.hzcx.app.simplechat.MyApplication;
import com.hzcx.app.simplechat.common.CommonSp;
import com.hzcx.app.simplechat.ui.login.bean.LoginInfoBean;
import com.hzcx.app.simplechat.ui.user.bean.UserInfoBean;
import com.hzcx.app.simplechat.util.empty.EmptyUtils;
import com.hzcx.app.simplechat.util.sp.SPUtils;

import org.litepal.LitePal;

public class UserInfoUtil {

    /**
     * 保存用户信息
     *
     * @param bean
     */
    public static void saveUser(LoginInfoBean bean) {
        SPUtils.put(MyApplication.getContext(), CommonSp.USER_ID, bean.getId());
        SPUtils.put(MyApplication.getContext(), CommonSp.USER_NICK_NAME, bean.getNickname());
        SPUtils.put(MyApplication.getContext(), CommonSp.USER_HEAD, bean.getAvatar());
        SPUtils.put(MyApplication.getContext(), CommonSp.USER_PHONE, bean.getMobile());
        SPUtils.put(MyApplication.getContext(), CommonSp.USER_HX_ID, bean.getHxusername());
    }

    /**
     * 保存用户信息
     *
     * @param bean
     */
    public static void saveUser(UserInfoBean bean) {
        try {
            SPUtils.put(MyApplication.getContext(), CommonSp.USER_ID, bean.getMember_id());
            SPUtils.put(MyApplication.getContext(), CommonSp.USER_NICK_NAME, bean.getNickname());
            SPUtils.put(MyApplication.getContext(), CommonSp.USER_HEAD, bean.getAvatar());
            SPUtils.put(MyApplication.getContext(), CommonSp.USER_PHONE, bean.getMobile());
            SPUtils.put(MyApplication.getContext(), CommonSp.USER_SIGN, bean.getBio());
            SPUtils.put(MyApplication.getContext(), CommonSp.USER_SEX, bean.getGender());
            SPUtils.put(MyApplication.getContext(), CommonSp.USER_NUMBER, bean.getChatnumber());
            SPUtils.put(MyApplication.getContext(), CommonSp.USER_ADDRESS, bean.getArea());
            SPUtils.put(MyApplication.getContext(), CommonSp.USER_BIRTHDAY, bean.getBirthday());
            SPUtils.put(MyApplication.getContext(), CommonSp.USER_PHONE_CITY_CODE, bean.getArea_code());
            SPUtils.put(MyApplication.getContext(), CommonSp.USER_BACKGROUND_IMG, bean.getBackimage());
            SPUtils.put(MyApplication.getContext(), CommonSp.USER_HX_ID, bean.getHxusername());
//            SPUtils.put(MyApplication.getContext(), CommonSp.USER_INFO_JSON, new Gson().toJson(bean));
            LitePal.deleteAll(UserInfoBean.class);
            bean.save();
            SPUtils.put(MyApplication.getContext(), CommonSp.USER_AUTO_SEND_MESSAGE, bean.getIs_auto());
            SPUtils.put(MyApplication.getContext(), CommonSp.USER_QRCODE, bean.getQrcodeimage());
            SPUtils.put(MyApplication.getContext(), CommonSp.USER_IS_WECHAT, bean.getIs_wechat());
            SPUtils.put(MyApplication.getContext(), CommonSp.USER_IS_VIP, bean.getIs_plus());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 获取用户ID
     *
     * @return
     */
    public static int getUserId() {
        try {
            return (int) SPUtils.get(MyApplication.getContext(), CommonSp.USER_ID, 0);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return 0;
    }

    /**
     * 获取用户环信ID
     *
     * @return
     */
    public static String getUserXHId() {
        try {
            return (String) SPUtils.get(MyApplication.getContext(), CommonSp.USER_HX_ID, "");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "";
    }

    /**
     * 获取用户昵称
     *
     * @return
     */
    public static String getUserNickName() {
        try {
            return (String) SPUtils.get(MyApplication.getContext(), CommonSp.USER_NICK_NAME, "");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "";
    }

    /**
     * 获取用户头像
     *
     * @return
     */
    public static String getUserHead() {
        try {
            return (String) SPUtils.get(MyApplication.getContext(), CommonSp.USER_HEAD, "");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "";
    }

    /**
     * 获取用户手机号
     *
     * @return
     */
    public static String getUserPhone() {
        try {
            return (String) SPUtils.get(MyApplication.getContext(), CommonSp.USER_PHONE, "");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "";
    }

    /**
     * 获取用户性别
     *
     * @return
     */
    public static String getUserSex() {
        try {
            return (String) SPUtils.get(MyApplication.getContext(), CommonSp.USER_SEX, "");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "";
    }

    /**
     * 获取用户编号
     *
     * @return
     */
    public static String getUserNumber() {
        try {
            return (String) SPUtils.get(MyApplication.getContext(), CommonSp.USER_NUMBER, "");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "";
    }

    /**
     * 获取用户地址
     *
     * @return
     */
    public static String getUserAddress() {
        try {
            return (String) SPUtils.get(MyApplication.getContext(), CommonSp.USER_ADDRESS, "");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "";
    }

    /**
     * 获取用户签名
     *
     * @return
     */
    public static String getUserSign() {
        try {
            return (String) SPUtils.get(MyApplication.getContext(), CommonSp.USER_SIGN, "");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "";
    }

    /**
     * 获取用户出生日期
     *
     * @return
     */
    public static String getUserBir() {
        try {
            return (String) SPUtils.get(MyApplication.getContext(), CommonSp.USER_BIRTHDAY, "");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "";
    }

    /**
     * 获取用户手机区号
     *
     * @return
     */
    public static String getUserPhoneCityCode() {
        try {
            return (String) SPUtils.get(MyApplication.getContext(), CommonSp.USER_PHONE_CITY_CODE, "");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "";
    }

    /**
     * 获取用户背景墙
     *
     * @return
     */
    public static String getUserBackgroundImg() {
        try {
            return (String) SPUtils.get(MyApplication.getContext(), CommonSp.USER_BACKGROUND_IMG, "");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "";
    }

    /**
     * 获取用户是否自动打招呼
     *
     * @return
     */
    public static int getUserAutoSendMessage() {
        try {
            return (int) SPUtils.get(MyApplication.getContext(), CommonSp.USER_AUTO_SEND_MESSAGE, 0);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return 0;
    }

    /**
     * 获取用户二维码图片
     *
     * @return
     */
    public static String getUserQrCodeImg() {
        try {
            return (String) SPUtils.get(MyApplication.getContext(), CommonSp.USER_QRCODE, "");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "";
    }

    /**
     * 获取用户是否绑定微信
     *
     * @return
     */
    public static int getUserIsWechat() {
        try {
            return (int) SPUtils.get(MyApplication.getContext(), CommonSp.USER_IS_WECHAT, 0);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return 0;
    }

    /**
     * 获取用户是否是会员
     *
     * @return
     */
    public static boolean getUserIsVip() {
        try {
            int isVip = (int) SPUtils.get(MyApplication.getContext(), CommonSp.USER_IS_VIP, 0);
            return isVip == 1 ? true : false;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }

    /**
     * 设置用户是否是会员
     *
     * @param isVip
     */
    public static void updateUserVip(int isVip) {
        SPUtils.put(MyApplication.getContext(), CommonSp.USER_IS_VIP, isVip);
    }

    /**
     * 获取用户信息
     *
     * @return
     */
    public static UserInfoBean getUserInfo() {
//        try {
//            String json = SPUtils.get(MyApplication.getContext(), CommonSp.USER_INFO_JSON, "").toString();
//            if (EmptyUtils.isEmpty(json)) {
//                return null;
//            }
//            return new Gson().fromJson(json, UserInfoBean.class);
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
//        return null;
        try {
            UserInfoBean bean = LitePal.findFirst(UserInfoBean.class, true);
            return bean;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

}
